/**
 * 
 */
package com.m7012e.a_remote.ar_stub;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.leoj.utils.tcp_network.tcp_server.SingleThreadTcpServer;
import com.m7012e.a_remote.android_remote.network.protocol.DiscoveryHandling.DaemonDiscoveryProtocolFactory;
import com.m7012e.a_remote.android_remote.network.protocol.echoProtocol.DaemonListingProtocolFactory;
import com.m7012e.a_remote.commons.ARemoteMulticastSender;
import com.m7012e.a_remote.communication.DisplayDiscoveryMessage;
import com.m7012e.a_remote.communication.positioning.SimplePosition;

/**
 * @author leoj
 *
 */
public class ARemoteSimulatorLauncher {
		/**
	 * @param args
		 * @throws IOException 
	 */
	public static void mainV1() throws IOException {
		//we need:
		
		//to be placed somewhere
		final SimplePosition remotePosition = new SimplePosition();
		System.out.println("new remote at the position : "+remotePosition);
		//to have a tcpProtocol
		final DaemonListingProtocolFactory factory = new DaemonListingProtocolFactory();
		
		//to have a TcpServer running
		final SingleThreadTcpServer tcpServer = new SingleThreadTcpServer(8707, factory);
		System.out.println("starting a singleThreadTcpServer : @"+tcpServer.getServerStrAddress()+":"+tcpServer.getServerPort());
		//That's it for now ... 
		//We just need to start the server and launch the listing by multicasting a UDP message
		tcpServer.start();
		
		new Thread(){
			public void run() {
				try {
					DisplayDiscoveryMessage msg = new DisplayDiscoveryMessage(tcpServer.getServerStrAddress(), tcpServer.getServerPort(), remotePosition);
					ARemoteMulticastSender.INSTANCE.sendMessage(msg);
					factory.startListing();
					System.out.println("to print the listing press enter");
					InputStreamReader inputReader = new InputStreamReader(System.in);
					BufferedReader reader = new BufferedReader(inputReader);
					while(true){
						reader.readLine();
						System.out.println(factory.printCurrentListing());
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			};
		}.start();
	}
	
	public static void mainV2() throws IOException{
		final SimplePosition remotePosition = new SimplePosition();
		System.out.println("new remote at the position : "+remotePosition);
		
		final DaemonDiscoveryProtocolFactory factory;
	}

	public static void main(String[] args) throws IOException{
//		mainV1();
		mainV2();
	}
}
